Implement ostree::checksum_file
authorFelix Krull <f_krull@gmx.de>
Mon, 2 Sep 2019 10:22:58 +0000 (12:22 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:54 +0000 (12:53 -0400)
rust-bindings/rust/src/functions.rs [new file with mode: 0644]
rust-bindings/rust/src/lib.rs
rust-bindings/rust/src/object_name.rs
rust-bindings/rust/src/repo.rs
rust-bindings/rust/tests/functions/mod.rs [new file with mode: 0644]
rust-bindings/rust/tests/tests.rs

diff --git a/rust-bindings/rust/src/functions.rs b/rust-bindings/rust/src/functions.rs
new file mode 100644 (file)
index 0000000..d3a20ff
--- /dev/null
@@ -0,0 +1,34 @@
+use crate::{Checksum, ObjectType};
+use glib::prelude::*;
+use glib::translate::*;
+use glib_sys::GFALSE;
+use std::error::Error;
+use std::ptr;
+
+pub fn checksum_file<P: IsA<gio::File>, Q: IsA<gio::Cancellable>>(
+    f: &P,
+    objtype: ObjectType,
+    cancellable: Option<&Q>,
+) -> Result<Checksum, Box<dyn Error>> {
+    unsafe {
+        let mut out_csum = ptr::null_mut();
+        let mut error = ptr::null_mut();
+        let ret = ostree_sys::ostree_checksum_file(
+            f.as_ref().to_glib_none().0,
+            objtype.to_glib(),
+            &mut out_csum,
+            cancellable.map(|p| p.as_ref()).to_glib_none().0,
+            &mut error,
+        );
+
+        if !error.is_null() {
+            Err(Box::<glib::Error>::new(from_glib_full(error)))
+        } else if ret == GFALSE {
+            Err(Box::new(glib_bool_error!(
+                "unknown error in ostree_checksum_file"
+            )))
+        } else {
+            Ok(Checksum::from_glib_full(out_csum))
+        }
+    }
+}
index 274b479885af250fc2bcc1192d0b4ca5d2fbe32b..f35ff5c6f3acee3b9198b159e3d138ccb13c0c88 100644 (file)
@@ -37,6 +37,7 @@ pub use crate::auto::*;
 mod checksum;
 #[cfg(any(feature = "v2018_6", feature = "dox"))]
 mod collection_ref;
+mod functions;
 #[cfg(any(feature = "v2019_3", feature = "dox"))]
 mod kernel_args;
 mod object_name;
@@ -47,6 +48,7 @@ mod se_policy;
 pub use crate::checksum::*;
 #[cfg(any(feature = "v2018_6", feature = "dox"))]
 pub use crate::collection_ref::*;
+pub use crate::functions::*;
 #[cfg(any(feature = "v2019_3", feature = "dox"))]
 pub use crate::kernel_args::*;
 pub use crate::object_name::*;
index 35998a41e1d8b4e2cbe905801e215215995cad7d..098c7f9add2f2a3f71fa1ca1e23b37b022312a5d 100644 (file)
@@ -1,4 +1,4 @@
-use functions::{object_name_deserialize, object_name_serialize, object_to_string};
+use crate::{object_name_deserialize, object_name_serialize, object_to_string};
 use glib;
 use glib::translate::*;
 use glib::GString;
index a6b2aaff967b68c1ab188d30bb987a833ba00f4c..3098ce2d7fe239df60fbf696029b42838fbd7887 100644 (file)
@@ -11,6 +11,7 @@ use glib::Error;
 use glib::IsA;
 use glib_sys;
 use ostree_sys;
+#[cfg(feature = "futures")]
 use std::boxed::Box as Box_;
 use std::collections::{HashMap, HashSet};
 use std::path::Path;
diff --git a/rust-bindings/rust/tests/functions/mod.rs b/rust-bindings/rust/tests/functions/mod.rs
new file mode 100644 (file)
index 0000000..196c21b
--- /dev/null
@@ -0,0 +1,21 @@
+use gio::NONE_CANCELLABLE;
+use glib::Cast;
+use ostree::{checksum_file, ObjectType, RepoFile, RepoFileExt};
+use util::TestRepo;
+
+#[test]
+fn should_checksum_file() {
+    let repo = TestRepo::new();
+    repo.test_commit("test");
+
+    let file = repo
+        .repo
+        .read_commit("test", NONE_CANCELLABLE)
+        .expect("read commit")
+        .0
+        .downcast::<RepoFile>()
+        .expect("downcast");
+    let result = checksum_file(&file, ObjectType::File, NONE_CANCELLABLE).expect("checksum file");
+
+    assert_eq!(file.get_checksum().unwrap(), result.to_string());
+}
index 3a8502de28c4759e6c1ce9a8e0f5bdf0fcb68395..2e6f717824d92fc7926234295e55efbc060e86d8 100644 (file)
@@ -6,5 +6,6 @@ extern crate tempfile;
 #[macro_use]
 extern crate maplit;
 
+mod functions;
 mod repo;
 mod util;